//
//  SEExamineReserveProjectCell.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/8.
//

import UIKit

protocol SEExamineReserveProjectCellDelegate: NSObjectProtocol {
    func examineReserveProjectCellItemAction(indexPath: IndexPath)
}

class SEExamineReserveProjectCell: UITableViewCell {

    static let className = "SEExamineReserveProjectCell"
    static let reuseId = className

    @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!

    weak var delegate: SEExamineReserveProjectCellDelegate?

    var viewModel: SEExamineReserveViewModel? {
        didSet {
            collectionView.reloadData()
            let contentSize = collectionView.collectionViewLayout.collectionViewContentSize
            collectionViewHeightConstraint.constant = contentSize.height
        }
    }

    @IBOutlet weak var collectionView: UICollectionView! {
        didSet {
            collectionView.register(UINib(nibName: SEExamineReserveProjectCvCell.className, bundle: nil), forCellWithReuseIdentifier: SEExamineReserveProjectCvCell.reuseId)
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    func updateCell(vm: SEExamineReserveViewModel) {
        viewModel = vm
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}

// MARK: - UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout

extension SEExamineReserveProjectCell: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        guard let vm = viewModel else {
            return 0
        }
        return vm.checkItemArray.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: SEExamineReserveProjectCvCell.reuseId, for: indexPath) as! SEExamineReserveProjectCvCell
        if let vm = viewModel {
            cell.updateCell(itemModel: vm.checkItem(indexPath: indexPath))
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: (UIScreen.screenWidth - 60) / 4.0, height: 70)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        delegate?.examineReserveProjectCellItemAction(indexPath: indexPath)
    }
}

